home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 223_01 / cc41.c < prev    next >
Text File  |  1980-01-01  |  7KB  |  366 lines

  1. /*
  2. ** print all assembler info before any code is generated
  3. */
  4. header()  {
  5.   beglab=getlabel();
  6.                              /*42*/
  7.   }
  8.  
  9. /*
  10. ** print any assembler stuff needed at the end
  11. */
  12. trailer()  {  
  13. #ifndef LINK
  14.   if((beglab == 1)|(beglab > 9000)) {                  /*51*/
  15.     /* implementation dependent trailer code goes here */
  16.     }
  17. #else
  18.   char *ptr;            /*54*/
  19.   cptr=STARTGLB;                                       /*37*/
  20.   while(cptr<ENDGLB) {                                 /*37*/
  21.     if(cptr[IDENT]==FUNCTION && cptr[CLASS]==AUTOEXT)  /*37*/
  22.       external(cptr+NAME);                             /*37*/
  23.     cptr+=SYMMAX;                                      /*37*/
  24.     }                                                  /*37*/
  25. #ifdef UPPER
  26.   if((ptr=findglb("MAIN")) && (ptr[OFFSET]==FUNCTION)){ /*54*/
  27. #else
  28.   if((ptr=findglb("main")) && (ptr[OFFSET]==FUNCTION)){ /*54*/
  29. #endif
  30.     external("Ulink");  /* link to library functions *//*33*/
  31. #endif
  32.  
  33.     if(nbflg)            /* return to CCP ? */
  34.        ol("ZZZCCP::  DB  1");
  35.     else            /* normal return   */
  36.        ol("ZZZCCP::  DB  0");
  37.     }
  38.   ol("END");
  39.   }
  40.  
  41. /*
  42. ** load # args before function call
  43. */
  44. loadargc(val) int val; {
  45.   if(search("NOCCARGC", macn, NAMESIZE+2, MACNEND, MACNBR, 0)==0) {
  46.     if(val) {                  /*35*/
  47.       ot("MVI A,");
  48.       outdec(val);
  49.       nl();
  50.       }                        /*35*/
  51.     else ol("XRA A");          /*35*/
  52.     }
  53.   }
  54.  
  55. /*
  56. ** declare entry point
  57. */
  58. entry() {
  59.   outstr(ssname);
  60.   col();
  61. #ifdef LINK
  62.   if(m80flg) col();                        /*28*//* fas 2.2 */
  63. #endif
  64.   nl();
  65.   }
  66.  
  67. /*
  68. ** declare external reference
  69. */
  70. external(name) char *name; {
  71. #ifdef LINK
  72.   ot("EXT ");
  73.   ol(name);
  74. #endif
  75.   }
  76.  
  77. /*
  78. ** fetch object indirect to primary register
  79. */
  80. indirect(lval) int lval[]; {
  81.   char *sym, indlevel;                    /* fas 2.4 */
  82.   sym = lval[0];                    /* fas 2.4 */
  83.   indlevel = sym[LEVEL];                /* fas 2.4 */
  84.   if(indlevel){                        /* fas 2.4 */
  85.     if(sym[CLASS] == AUTOMATIC) indlevel++;        /* fas 2.4 */ 
  86.     if(level++ >= indlevel && sym[IDENT] == POINTER)    /* fas 2.4 */
  87.       lval[1] = sym[ITYPE];                /* fas 2.4 */
  88.     if(level >= indlevel) explevel = TRUE;        /* fas 2.4 */
  89.     }
  90.   if(lval[1]==CCHAR) ffcall("CCGCHAR##");
  91.   else               ffcall("CCGINT##");
  92.   }
  93.  
  94. /*
  95. ** fetch a static memory cell into primary register
  96. */
  97. getmem(lval)  int lval[]; {
  98.   char *sym;
  99.   sym=lval[0];
  100.   if((sym[IDENT]!=POINTER)&(sym[TYPE]==CCHAR)) {
  101.     ot("LDA ");
  102.     outstr(sym+NAME);
  103.     nl();
  104.     ffcall("CCSXT##");
  105.     }
  106.   else {
  107.     ot("LHLD ");
  108.     outstr(sym+NAME);
  109.     nl();
  110.     }
  111.   }
  112.  
  113. /*
  114. ** fetch addr of the specified symbol into primary register
  115. */
  116. getloc(sym)  char *sym; {
  117.   const(getint(sym+OFFSET, OFFSIZE)-csp);
  118.   ol("DAD SP");
  119.   }
  120.  
  121. /*
  122. ** store primary register into static cell
  123. */
  124. putmem(lval)  int lval[]; {
  125.   char *sym;
  126.   sym=lval[0];
  127.   if((sym[IDENT]!=POINTER)&(sym[TYPE]==CCHAR)) {
  128.     ol("MOV A,L");
  129.     ot("STA ");
  130.     }
  131.   else ot("SHLD ");
  132.   outstr(sym+NAME);
  133.   nl();
  134.   }
  135.  
  136. /*
  137. ** put on the stack the type object in primary register
  138. */
  139. putstk(lval) int lval[]; {
  140.   char *sym;                        /* fas 2.4 */
  141.   if(explevel){                        /* fas 2.4 */
  142.     sym = lval[0];                    /* fas 2.4 */
  143.     lval[1] = sym[ITYPE];                /* fas 2.4 */
  144.     explevel = 0;                    /* fas 2.4 */
  145.     }
  146.   if(lval[1]==CCHAR) {
  147.     ol("MOV A,L");
  148.     ol("STAX D");
  149.     }
  150.   else ffcall("CCPINT##");
  151.   }
  152.  
  153. /*
  154. ** move primary register to secondary
  155. */
  156. move() {
  157.   ol("MOV D,H");
  158.   ol("MOV E,L");
  159.   }
  160.  
  161. /*
  162. ** swap primary and secondary registers
  163. */
  164. swap() {
  165.   ol("XCHG;;");  /* peephole() uses trailing ";;" */
  166.   }
  167.  
  168. /*
  169. ** swap prim. and sec. registers but don't allow optimizing  fas 2.6
  170. */
  171. swap2() {
  172.   ol("XCHG");
  173.   }
  174.  
  175. /*
  176. ** partial instruction to get immediate value
  177. ** into the primary register
  178. */
  179. immed() {
  180.   ot("LXI H,");
  181.   }
  182.  
  183. /*
  184. ** partial instruction to get immediate operand
  185. ** into secondary register
  186. */
  187. immed2() {
  188.   ot("LXI D,");
  189.   }
  190.  
  191. /*
  192. ** push primary register onto stack
  193. */
  194. push() {
  195.   ol("PUSH H");
  196.   csp=csp-BPW;
  197.   }
  198.  
  199. /*
  200. ** unpush or pop as required
  201. */
  202. smartpop(lval, start) int lval[]; char *start; {
  203.   if(lval[5])  pop(); /* secondary was used */
  204.   else unpush(start);
  205.   }
  206.  
  207. /*
  208. ** replace a push with a swap
  209. */
  210. unpush(dest) char *dest; {
  211.   int i;
  212.   char *sour;
  213.   sour="XCHG;;";  /* peephole() uses trailing ";;" */
  214.   while(*sour) *dest++ = *sour++;
  215.   sour=stagenext;
  216.   while(--sour > dest) { /* adjust stack references */
  217.     if(streq(sour,"DAD SP")) {
  218.       --sour;
  219.       i=BPW;
  220.       while(isdigit(*(--sour))) {
  221.         if((*sour = *sour-i) < '0') {
  222.           *sour = *sour+10;
  223.           i=1;
  224.           }
  225.         else i=0;
  226.         }
  227.       }
  228.     }
  229.   csp=csp+BPW;
  230.   }
  231.  
  232. /*
  233. ** pop stack to the secondary register
  234. */
  235. pop() {
  236.   ol("POP D");
  237.   csp=csp+BPW;
  238.   }
  239.  
  240. /*
  241. ** swap primary register and stack
  242. */
  243. swapstk() {
  244.   ol("XTHL");
  245.   }
  246.  
  247. /*
  248. ** process switch statement
  249. */
  250. sw() {
  251.   ffcall("CCSWITCH##");
  252.   }
  253.  
  254. /*
  255. ** call specified subroutine name
  256. */
  257. ffcall(sname)  char *sname; {
  258.   ot("CALL ");
  259.   outstr(sname);
  260.   nl();
  261.   }
  262.  
  263. /*
  264. ** return from subroutine
  265. */
  266. ffret() {
  267.   ol("RET");
  268.   }
  269.  
  270. /*
  271. ** perform subroutine call to value on stack
  272. */
  273. callstk() {
  274.   ffcall("CCDCAL##");                     /*36*/
  275.   }
  276.  
  277. /*
  278. ** jump to internal label number
  279. */
  280. jump(label)  int label; {
  281.   ot("JMP ");
  282.   printlabel(label);
  283.   nl();
  284.   }
  285.  
  286. /*
  287. ** test primary register and jump if false
  288. */
  289. testjump(label)  int label; {
  290.   ol("MOV A,H");
  291.   ol("ORA L");
  292.   ot("JZ ");
  293.   printlabel(label);
  294.   nl();
  295.   }
  296.  
  297. /*
  298. ** test primary register against zero and jump if false
  299. */
  300. zerojump(oper, label, lval) int (*oper)(), label, lval[]; { /*13*/
  301.   clearstage(lval[7], 0);  /* purge conventional code */
  302.   (*oper)(label);                                           /*13*/
  303.   }
  304.  
  305. /*
  306. ** define storage according to size
  307. */
  308. defstorage(size) int size; {
  309.   if(size==1) ot("DB ");
  310.   else        ot("DW ");
  311.   }
  312.  
  313. /*
  314. ** point to following object(s)
  315. */
  316. point() {
  317.   ol("DW $+2");
  318.   }
  319.  
  320. /*
  321. ** modify stack pointer to value given
  322. */
  323. modstk(newsp, save)  int newsp, save; {
  324.   int k;
  325.   k=newsp-csp;
  326.   if(k==0)return newsp;
  327.   if(k>=0) {
  328.     if(k<7) {
  329.       if(k&1) {
  330.         ol("INX SP");
  331.         k--;
  332.         }
  333.       while(k) {
  334.         ol("POP B");
  335.         k=k-BPW;
  336.         }
  337.       return newsp;
  338.       }
  339.     }
  340.   if(k<0) {
  341.     if(k>-7) {
  342.       if(k&1) {
  343.         ol("DCX SP");
  344.         k++;
  345.         }
  346.       while(k) {
  347.         ol("PUSH B");
  348.         k=k+BPW;
  349.         }
  350.       return newsp;
  351.       }
  352.     }
  353.   if(save) swap();
  354.   const(k);
  355.   ol("DAD SP");
  356.   ol("SPHL");
  357.   if(save) swap();
  358.   return newsp;
  359.   }
  360.  
  361. /*
  362. ** double primary register
  363. */
  364. doublereg() {ol("DAD H");}
  365.  
  366.